package com.leetcode.partition1;

import java.util.*;

/**
 * @author `RKC`
 * @date 2021/8/3 8:50
 */
public class LC1两数之和 {

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> numberMap = new HashMap<>();
        int[] result = new int[2];
        //遍历nums，将其以值为key，索引为value的形式存放在哈希表
        for (int i = 0; i < nums.length; i++) {
            numberMap.put(nums[i], i);
        }
        //遍历nums，并与target进行作差，差值在哈希表中进行查找
        for (int i = 0; i < nums.length; i++) {
            int differ = target - nums[i];
            if (numberMap.get(differ) == null) continue;
            if (numberMap.get(differ) != i) {
                result[0] = i;
                result[1] = numberMap.get(differ);
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {2,5,5,11};
        int target = 10;
        System.out.println(Arrays.toString(twoSum(nums, target)));
    }
}